<template>
  <div class="wrap">
    <el-form
      :inline="true"
      :model="ruleform"
      ref="from"
      :rules="rules"
      class="demo-form-inline ip-form"
    >
      <el-form-item label="邮箱 " prop="email">
        <el-input v-model="ruleform.email" message placeholder></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="onSubmit('from')"
          >注册</el-button
        >
        <el-button type="warning" plain @click="onReset('from')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import { resetForm } from "@/utils/Tool";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    // 自定义验证邮箱
    var checkEmail = (rule, value, callback) => {
      const regEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
      if (regEmail.test(value)) {
        console.log(regEmail.test(value));
        return callback();
      } else {
        callback(new Error("请输入合法邮箱"));
        //callback();
      }
    };
    return {
      ruleform: {
        email: "",
      },
      rules: {
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
      },
    };
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    //添加 提交
    onSubmit(formName) {
      var that = this;
      that.$refs[formName].validate((valid) => {
        if (that.ruleform.email == "") {
          return;
        }
        if (valid) {
          postData("/adminweb/dev/register", {
            email: that.ruleform.email,
          })
            .then((res) => {
              if (res.code == 0) {
                that.$message.success("注册成功！");
                //刷新table
                that.$store.commit(
                  "setSubmitData",
                  Math.floor(Math.random() * 10000000)
                );
                let datas = resetForm(that.ruleform, this.$refs[formName]);
                that.ruleform = datas;
              } else if (res.code == -56055) {
                return;
              } else {
                this.$message.error(res.code + "，" + res.msg);
              }
            })
            .catch((res) => {
              this.$message.error(res.code + "，" + res.msg);
            });
        } else {
          that.$message.error("注册失败");
          return false;
        }
      });
    },
    //重置
    onReset(val) {
      this.$refs[val].resetFields();
    },
  },
  watch: {},
};
</script>
<style scoped>
.wrap {
  padding: 15px;
}
</style>
